<?php
declare (strict_types=1);

namespace app\admin\model\content\agreement;

use app\admin\logic\user\Account;
use app\common\model\BaseModel;

/**
 * 定义协议数据模型
 */
class Agreement extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'content_agreement';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 协议keys
     *
     * @var array
     */
    protected $agreementKeysList = [
        [
            'key'   => 'userRegister',
            'title' => '用户注册协议',
            'type'  => 1 // 1:通用  2:平台  3:机构
        ],
        [
            'key'   => 'userUsing',
            'title' => '用户使用协议',
            'type'  => 1
        ],
        [
            'key'   => 'userPrivacy',
            'title' => '用户隐私协议',
            'type'  => 1
        ],
        [
            'key'   => 'payment',
            'title' => '支付协议',
            'type'  => 1
        ],
        [
            'key'   => 'schoolUser',
            'title' => '机构用户服务协议',
            'type'  => 2
        ],
        [
            'key'   => 'teacherUser',
            'title' => '讲师用户服务协议',
            'type'  => 1
        ],
        [
            'key'   => 'mobileVip',
            'title' => 'vip权益说明(手机端)',
            'type'  => 1
        ],
        [
            'key'   => 'aboutUs',
            'title' => '关于我们',
            'type'  => 1
        ]
    ];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param    [type]                         $agreement [description]
     */
    public static function onBeforeInsert($agreement)
    {
        $agreement->last_admin_user_id = request()->admin_user_id;
        // 机构
        $agreement->mhm_id = $agreement->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 更新事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param    [type]                         $agreement [description]
     */
    public static function onBeforeUpdate($agreement)
    {
        $agreement->last_admin_user_id = request()->admin_user_id;
    }

    /**
     * 最后操作者
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @return   [type]                         [description]
     */
    public function lastActionUser()
    {
        return $this->belongsTo(Account::class, 'last_admin_user_id');
    }

    /**
     * @return array
     */
    public function getAgreementKeysList(): array
    {
        return $this->agreementKeysList;
    }
}
